package cn.seqdata.domain;

import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Minutes;
import org.joda.time.Months;
import org.joda.time.Period;
import org.joda.time.ReadableInterval;
import org.joda.time.ReadablePeriod;
import org.joda.time.Weeks;
import org.joda.time.Years;

public enum DateInterval {
	pt0s {
		@Override
		public ReadablePeriod getPeriod() {
			return Period.ZERO;
		}

		@Override
		public ReadablePeriod getDuration() {
			return Period.ZERO;
		}
	},
	p1d {
		@Override
		public ReadablePeriod getPeriod() {
			return Minutes.minutes(15);
		}

		@Override
		public ReadablePeriod getDuration() {
			return Days.ONE;
		}

		@Override
		public ReadableInterval toInterval(LocalDate occur) {
			return occur.toInterval();
		}
	},
	p1w {
		@Override
		public ReadablePeriod getPeriod() {
			return Hours.FOUR;
		}

		@Override
		public ReadablePeriod getDuration() {
			return Weeks.ONE;
		}

		@Override
		public ReadableInterval toInterval(LocalDate occur) {
			return super.toInterval(occur.withDayOfWeek(1));
		}
	},
	p1m {
		@Override
		public ReadablePeriod getPeriod() {
			return Days.ONE;
		}

		@Override
		public ReadablePeriod getDuration() {
			return Months.ONE;
		}

		@Override
		public ReadableInterval toInterval(LocalDate occur) {
			return super.toInterval(occur.withDayOfMonth(1));
		}
	},
	p1y {
		@Override
		public ReadablePeriod getPeriod() {
			return Months.ONE;
		}

		@Override
		public ReadablePeriod getDuration() {
			return Years.ONE;
		}

		@Override
		public ReadableInterval toInterval(LocalDate occur) {
			return super.toInterval(occur.withDayOfYear(1));
		}
	};

	/** 每个数据的间隔时间 */
	public abstract ReadablePeriod getPeriod();

	/** 整个周期的持续时间 */
	public abstract ReadablePeriod getDuration();

	/** 给定日期，返回起止时间 */
	public ReadableInterval toInterval(LocalDate occur) {
		return new Interval(occur.toDateTimeAtStartOfDay(), getDuration());
	}
}
